<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* 
*/

class Adscate_model extends MY_Model {
    private $table = 'adscate';

    /**
     * 获取多条数据
     * @param  array
     * @param  boolean
     * @return [array]
     */
    public function get_lists() {
        return $this->db->order_by('sort ASC, id ASC')->get($this->table)->result_array();
    }

    /**
     * 获取所有数据总数
     * @param  array
     * @param  array
     * @return [int]
     */
    public function total_rows() {
        return $this->db->select('id')->get($this->table)->num_rows();
    }

    /**
     * 添加数据
     * @return [int]
     */
    public function insert() {
        $data = $this->get_data();
        if (empty($data)) {
            return 0;
        }
        
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 获取一行数据
     * @param  array
     * @param  array
     * @return [array]
     */
    public function get_one($where = [], $select = []) {
        if (empty($where)) {
            return [];
        }
        return $this->db->select($select)->get_where($this->table, $where)->row_array();
    }
    
    /**
     * 编辑数据
     * @param  array
     * @return [int]
     */
    public function update($where = []) {
        if (empty($where)) {
            return 0;
        }
        
        $data = $this->get_data();
        if (empty($data)) {
            return 0;
        }

        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }
    
    /**
     * 删除数据
     * @param  array
     * @return [int]
     */
    public function delete($where = []) {
        if (empty($where)) {
            return 0;
        }
        
        $this->db->delete($this->table, $where);
        return $this->db->affected_rows();
    }
}
